﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UsersForms
{
    public partial class Form4 : Form
    {
        public int Values { get; set; }

        public string Id { get; set; }

        public Form4()
        {
            InitializeComponent();
            Values = -1;
        }

        public Form4(string title, string author, string id)
        {
            InitializeComponent();

            Values = 1;

            textBox1.Text = title;
            textBox2.Text = author;
            Id = id;

        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            var title = textBox1.Text;
            var author = textBox2.Text;

            if (Values == -1)//添加
            {
                if (title != "" && author != "")
                {
                    string sql = string.Format("insert into Articles(Title,Author) values('{0}','{1}')", title, author);

                    var n = DBHelper.DeleteUpdateInsert(sql);

                    if (n >= 1)
                    {
                        MessageBox.Show("添加成功");

                        string sql2 = string.Format("select * from Articles where Title like '%{0}%'", textBox1.Text);

                        Form3.StrartForm.dataGridView1.DataSource = DBHelper.Select(sql2);//刷新数据

                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("添加失败");
                    }
                }
                else
                {
                    MessageBox.Show("输入的内容不能为空！！", "提示");
                }

            }
            else if (Values == 1)//更新
            {
                string sqlUpdate = string.Format("update Articles set Title='{0}',Author='{1}' where id='{2}'", title, author, Id);

                var n = DBHelper.DeleteUpdateInsert(sqlUpdate);

                if (n >= 1)
                {
                    MessageBox.Show("更新成功");

                    //刷新数据
                    if (Form3.StrartForm.comboBox1.SelectedItem != null)
                    {
                        if (Form3.StrartForm.comboBox1.SelectedItem.ToString() == "Title")
                        {
                            string sql = string.Format("select * from Articles where Title like '%{0}%'", textBox1.Text);

                            Form3.StrartForm.dataGridView1.DataSource = DBHelper.Select(sql);
                        }
                        else if (Form3.StrartForm.comboBox1.SelectedItem.ToString() == "Author")
                        {
                            string sql = string.Format("select * from Articles where Author like '%{0}%'", textBox1.Text);

                            Form3.StrartForm.dataGridView1.DataSource = DBHelper.Select(sql);
                        }
                    }
                    else
                    {
                        string sql = string.Format("select * from Articles");

                        Form3.StrartForm.dataGridView1.DataSource = DBHelper.Select(sql);
                    }


                    this.Close();
                }
                else
                {
                    MessageBox.Show("更新失败");
                }

            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
